﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;
using System.Drawing;
using Utilities.Security; 


public partial class Controls_Common_ctrLogin : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.InitContent(); 
        }
    }

    private void InitContent()
    {
        string sSession = ConvertData.ConvertToString(  Session[Config.SESSION_USEREMAIL]);
        if (sSession !="")
        {
            block.Visible = false;
            
        }
        else
        {
            block.Visible = true;
            int nCurrentLanguage = Support.GetCurrentLanguage();
            lblLogin.Text = MultipleLanguage.LBL_LOGIN[nCurrentLanguage - 1];
            lblUsername.Text = MultipleLanguage.EMAIL[nCurrentLanguage - 1];
            lblPassword.Text = MultipleLanguage.LBL_PASSWORD[nCurrentLanguage - 1];
          //  lnkForgotPass.Text = MultipleLanguage.FORGOT_PASSWORD[nCurrentLanguage - 1];
            lnkRegister.Text = MultipleLanguage.BTN_REGISTER[nCurrentLanguage - 1];
            btnLogin.Text = MultipleLanguage.LBL_LOGIN[nCurrentLanguage - 1];

            rfvEmail.ErrorMessage = MultipleLanguage.EMAIL_EMPTY[nCurrentLanguage - 1];
            rfvPassword.ErrorMessage = MultipleLanguage.PASSWORD_EMPTY[nCurrentLanguage - 1];
        }

      

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage()-1;
        bool isCheckUser = false;
        isCheckUser = isUser();
        if (isCheckUser)
        {
            lblMessage.Visible = false ;
            string sType = ConvertData.ConvertToString(Session[Config.SESSION_USER_TYPE]);
            Response.Redirect(Pages.FrontEnds.HOME); 
        }

        else
        {
            lblMessage.Text = MessagesAlert.LOGIN_FAIL[nDefaultLanguage];
            lblMessage.Visible = true;
            txtEmail.Focus();
        }
    }

    private bool isUser()
    {
        bool isUser = false;
        string sEmail = txtEmail.Text.Trim();
        string sUsername = "";
        int nUserID = 0;
        sEmail = sEmail.ToLower();
        string sPassword = txtPassword.Text.Trim();
        Users objUser = new Users();
        DataTable dtbUser = new DataTable();

        try
        {
            dtbUser = objUser.GetUserByEmail(sEmail, EnumeType.ACTIVE);
            string sCurrentEmail = ConvertData.ConvertToString(dtbUser.Rows[0]["Email"]);
            string sCurrentPassword = ConvertData.ConvertToString(dtbUser.Rows[0]["Password"]);
            sUsername = ConvertData.ConvertToString(dtbUser.Rows[0]["Username"]);
            nUserID = ConvertData.ConvertToInt (dtbUser.Rows[0]["UserID"]);
            string sUserType = ConvertData.ConvertToString(dtbUser.Rows[0]["RoleID"]);
            ClsEncrypt obj = new ClsEncrypt();
            string sDeCodePass = obj.Decrypt(sCurrentPassword);
            if (sEmail == sCurrentEmail && sPassword == sDeCodePass)
            {
                Session[Config.SESSION_USERNAME] = ConvertData.ConvertToString(sUsername);
                Session[Config.SESSION_USEREMAIL] = ConvertData.ConvertToString(sEmail);
                Session[Config.SESSION_USER_TYPE] = ConvertData.ConvertToString(sUserType);
                Session[Config.SESSION_USERID] = ConvertData.ConvertToString(nUserID);
                isUser = true;
            }
            else
                isUser = false;
        }
        catch (Exception )
        {
            isUser = false;
        }
        return isUser;
    }

    public string GetCurrentPageName()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;
        return sRet;
    } 
}
